﻿@{
    ViewBag.Title = "优惠活动";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/promotion")
<nav class="mod_nav">
    <div class="lay_left">
        <a class="nav_back" onclick="turnBack()"><img src="/images/return_1.png" ontouchstart="replaceBackImage(this, 1)" ontouchend="replaceBackImage(this, 2)"></a>
        <span id="promotionTitleHelp"></span>
    </div>
</nav>
<div class="promotionWindow">
    <div class="IndexBanner IndexPart" id="promotionImageBarner">
        <img id="promotionImage" alt="优惠活动" />
    </div>
    <section class="promtionLayout">
        <div id="goodsList" class="IndexGoods"></div>
    </section>
    <section id="content" class="promotionContentLayout">
        <div class="promotionContentTitle">
            <span>规则活动</span>
        </div>
        <div class="promotionContentBar">
            <div id="promotionContent"></div>
        </div>
    </section>
</div>
<section class="Shopping"></section>
<section class="OrderGray" onclick="hideShoppingCar()"></section>

<script>
    var scrollBegin = 0;

    window.onload = function () {
        loadShopping();
        setPromotionWindowHeight();
        var promotionGoodsInfo = PromotionMgr.getPromotionGoodsInfo();
        adjustPromotionBaseShow(promotionGoodsInfo);
        setPromotionGoodsListScrollListener();
        scrollBegin = 0;
        PromotionMgr.getPromotionGoodsListInfo(promotionGoodsInfo, scrollBegin, PromotionMgr.queryGoodsNum, getPromotionGoodsListCallback);
        adjustPromotionContentShow(promotionGoodsInfo);
    }

    function setPromotionGoodsListScrollListener() {
        var goodsWindow = $(".promtionLayout");
        var goodsDoc = $(".IndexGoods");
        isScroll(goodsWindow, goodsDoc, function () {
            scrollBegin = PromotionMgr.getPromotionGoodsListNum();
            if (scrollBegin > 0) {
                PromotionMgr.getPromotionGoodsListInfo(promotionGoodsInfo, scrollBegin, PromotionMgr.queryGoodsNum, getPromotionGoodsListCallback);
            }
        });
    }

    function turnBack() {
        window.location.href = '/Home/Index';
    }

    function adjustPromotionBaseShow(promotionGoodsInfo) {
        $("#promotionTitleHelp").text(promotionGoodsInfo.goods_info.name);
        var image = promotionGoodsInfo.goods_info.advert_image;
        if (image == null) {
            image = promotionGoodsInfo.goods_info.image;
        }
        $("#promotionImage").attr("src", image);
        var webViewUrl = promotionGoodsInfo.goods_info.webview_url;
        if (webViewUrl != null) {
            $("#promotionImage").on("click", function () {
                var url = '/Promotion/Iframe';
                window.location.href = url;
            });
        }
    }

    function adjustPromotionContentShow(promotionGoodsInfo) {
        if (promotionGoodsInfo.goods_info.good_content == undefined) {
            $("#content").hide();
        } else {
            $("#content").show();
            $("#promotionContent").html(promotionGoodsInfo.goods_info.good_content.replace(/\r\n/g, "<br/>"));
        }
    }

    function getPromotionGoodsListCallback(goodsList) {
        if (null != goodsList && goodsList.length > scrollBegin) {
            for (var i = scrollBegin; i < goodsList.length; i++) {
                var $div = $(
                    '<div class=\"IndexPart\">'
                        + '<div class=\"IndexGoodsImg\">'
                            + '<img class=\"GoodsImg\" src=\"' + goodsList[i].goods_info.image + '\">'
                        + '</div>'
                        + '<div class="IndexGoodsDetails">'
                            + '<div class=\"IndexGoodsTxt\">'
                            + '<span>' + goodsList[i].goods_info.name + '</span>'
                            + '</div>'
                            + '<div class="IndexGoodsPriceAndNum">'
                                + '<div class="IndexGoodsPrice">'
                                + '<span>￥' + goodsList[i].goods_info.price + '</span>'
                                + '</div>'
                                + '<div class=\"IndexGoodsNum\">'
                                    + '<a class=\"dec_count_' + goodsList[i].goods_info.goodsid + '\" onclick=\"adjustPromotionNum(' + goodsList[i].goods_info.goodsid + ', ' + goodsList[i].main_tab + ', ' + goodsList[i].sub_tab + ', -1)\"><img src=\"/images/Dec.png\" ontouchstart="replaceAddOrDecImage(this, 2)" ontouchend="replaceAddOrDecImage(this, 4)" ></a>'
                                    + '<span id=\"goodsid_' + goodsList[i].goods_info.goodsid + '\">' + goodsList[i].goods_num + '</span>'
                                    + '<a class=\"add_count_' + goodsList[i].goods_info.goodsid + '\" onclick=\"adjustPromotionNum(' + goodsList[i].goods_info.goodsid + ', ' + goodsList[i].main_tab + ', ' + goodsList[i].sub_tab + ', 1)\"><img src=\"/images/Add.png\" ontouchstart="replaceAddOrDecImage(this, 1)" ontouchend="replaceAddOrDecImage(this, 3)" ></a>'
                                + '</div>'
                            + '</div>'
                        + '</div>'
                    + '</div>');
                $("div#goodsList").append($div);
            }

            /*
                * CSS 不能动态的设置元素的宽度所以只能在这里通过JS设置元素的宽度
                * 商品列表中商品详情的宽度 图片、图片边线和左右边距 80 + 2 + 12 = 94
                */
            var indexGoodsDetailsWidth = $(window).width() - 94;
            $(".IndexGoodsDetails").css("width", indexGoodsDetailsWidth + "px");
            $(".IndexGoodsTxt").css("width", indexGoodsDetailsWidth + "px");
            $(".IndexGoodsPriceAndNum").css("width", indexGoodsDetailsWidth + "px");

            adjustGoodsList();

            scrollBegin = goodsList.length;
        }
    }

    function loadShopping() {
        var shoppingList = ShoppingMgr.getShoppingList();
        var url = '/Home/Shopping';
        NativeAjax(shoppingList, url, function (msg) {
            loadPartialViewResult(msg, $(".Shopping"), 1);
        });
    }

    function adjustGoodsList() {
        var goodsList = PromotionMgr.getPromotionGoodsList();
        $('.IndexPart .IndexGoodsNum').each(function (index) {
            var goodsNum = Number(goodsList[index].goods_num);
            if (goodsNum <= 0) {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).hide();
                $('.IndexPart .IndexGoodsNum span').eq(index).hide();
            } else {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).show();
                $('.IndexPart .IndexGoodsNum span').eq(index).show();
            }
        });
    }

    function adjustGoodsListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_count_" + goodsid).show();
            $("#goodsid_" + goodsid).show();
            $("#goodsid_" + goodsid).text(goodsNum);
        } else {
            $(".dec_count_" + goodsid).hide();
            $("#goodsid_" + goodsid).hide();
        }
    }

    function adjustPromotionGoodsNumShow(goodsid, goodsNum, shoppingList) {
        adjustGoodsListShow(goodsid, goodsNum);
        loadShopping();
    }

    function adjustPromotionNum(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustPromotionGoodsNumShow);
    }
    function setPromotionWindowHeight() {
        //当前浏览器窗口高度 - 购物车高度 Nav高度
        var currentWindowHeight = $(window).height() - 56 - 48;
        //当前容器确定高度
        $(".promotionWindow").css("height", currentWindowHeight + "px");
    }

</script>